/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cip.esymons.general.bc;

import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.cip.esymons.general.be.PersonaNatural;

/**
 *
 * @author OscarA
 */
@Stateless
public class PersonaNaturalFacade implements PersonaNaturalFacadeLocal {

    @PersistenceContext
    private EntityManager em;

    public void create(PersonaNatural personaNatural) {
        try {
            em.persist(personaNatural);
            em.createNativeQuery("commit;").executeUpdate();
            em.flush();
        } catch (Exception e) {
        }

    }

    //hecho por lizardo
    public void registrarPersona(PersonaNatural personaNatural) {
        em.persist(personaNatural);
    }

    //hecho por lizardo
    public PersonaNatural buscarPersona(String paterno, String materno, String nombres) {
        try {
            return (PersonaNatural) em.createQuery("select per from PersonaNatural per where per.perapellidoPaterno=?1 and per.perapellidoMaterno =?2 and per.perNombres =?3")
                    .setParameter(1, paterno)
                    .setParameter(2, materno)
                    .setParameter(3, nombres)
                    .getSingleResult();
        } catch (Exception ex) {
            System.out.println("FATAL: " + ex.getMessage());
            return new PersonaNatural();
        }
    }

    public void edit(PersonaNatural personaNatural) {
        em.merge(personaNatural);
    }

    public void remove(PersonaNatural personaNatural) {
        em.remove(em.merge(personaNatural));
    }

    public PersonaNatural find(Object id) {
        try {
            return em.find(PersonaNatural.class, id);
        } catch (Exception e) {
            return null;
        }

    }

    public List<PersonaNatural> findAll() {
        return em.createQuery("select object(o) from PersonaNatural as o").getResultList();
    }

    public PersonaNatural buscarXDni_Tipo(String dni) {
        try {
            return (PersonaNatural) em.createQuery("select object(o) from PersonaNatural as o where o.perNroDNI=?1 and o.perTipo = 0").setParameter(1, dni).getSingleResult();
        } catch (Exception ex) {
            return null;
        }
    }
    public PersonaNatural buscarXDni(String dni) {
        try {
            return (PersonaNatural) em.createQuery("select object(o) from PersonaNatural as o where o.perNroDNI=?1").setParameter(1, dni).getSingleResult();
        } catch (Exception ex) {
            System.out.println("FATAL: " + ex.getMessage());
            return null;            
        }
    }

    public PersonaNatural buscarXDni1(String dni) {
        try {
            return (PersonaNatural) em.createQuery("select per from PersonaNatural per where per.perNroDNI=?1")
                                        .setParameter(1, dni)
                                        .getSingleResult();
        } catch (Exception ex) {
            System.out.println("FATAL: " + ex.getMessage());
            return new PersonaNatural();
        }
    }

    public PersonaNatural buscarXPasaporte(String pasaporte) {
        try {
            return (PersonaNatural) em.createQuery("select object(o) from PersonaNatural as o where o.perNroPasaporte=?1").setParameter(1, pasaporte).getSingleResult();
        } catch (Exception ex) {
            return null;
        }
    }

    public PersonaNatural buscarXCodigo(Long codigo) {
        return (PersonaNatural) em.createQuery("select object(o) from PersonaNatural as o where o.solCodigo=?1").setParameter(1, codigo).getSingleResult();
    }

    public List<PersonaNatural> buscarXApellidosyNombres(String paterno, String materno, String nombres) {
        try  {
            return em.createQuery("select p from PersonaNatural p where p.perapellidoPaterno like ?1 and p.perapellidoMaterno like ?2 and p.perNombres like ?3")
                    .setParameter(1, paterno + "%")
                    .setParameter(2, materno + "%")
                    .setParameter(3, nombres + "%")
                    .getResultList();
        } catch (Exception ex) {
            System.out.println("FATAL: " + ex.getMessage());
            return null;
        }
    }

    public List<PersonaNatural> listarPersonas() {
        return em.createQuery("select p from PersonaNatural p where p.perActivo = true order by p.perapellidoPaterno, p.perapellidoMaterno, p.perNombres").getResultList();
    }

    public List<PersonaNatural> listarPersonasPorFechaIncorp(Date fecha1, Date fecha2) {

        return em.createQuery("select object(o) from PersonaNatural as o where o.colegiado.colfechaIncorporacion between ?1 and ?2 order by o.perapellidoPaterno").setParameter(1, fecha1).setParameter(2, fecha2).getResultList();
    }

    public List<PersonaNatural> listarPersonasPorFechaAporte(Date fecha1, Date fecha2) {

        return em.createQuery("select object(o) from PersonaNatural as o where o.colegiado.colfechaAporte between ?1 and ?2 order by o.perapellidoPaterno").setParameter(1, fecha1).setParameter(2, fecha2).getResultList();
    }

    public List<PersonaNatural> listarPersonasHabiles() {
        
        return em.createQuery("select object(o) from PersonaNatural as o where o.colegiado.tipoColegiado.tcoCodigo = 1 order by o.perapellidoPaterno").getResultList();
    }

    public List<PersonaNatural> listarPersonasConCorreo() {

        return em.createQuery("select object(o) from PersonaNatural as o " +
                " where o.colegiado.tipoColegiado.tcoCodigo = 1" +
                " and o.solcorreopersonal1 like '%@%' " +
                " order by o.perapellidoPaterno").getResultList();
    }

    public List<PersonaNatural> listarPersonasConCorreoFechaIncorp(Date fecha1, Date fecha2) {

        return em.createQuery("select object(o) from PersonaNatural as o " +
                " where o.colegiado.tipoColegiado.tcoCodigo = 1" +
                " and o.solcorreopersonal1 like '%@%' " +
                " and o.colegiado.colfechaIncorporacion  between ?1 and ?2 " +
                " order by o.perapellidoPaterno").setParameter(1, fecha1).setParameter(2, fecha2).getResultList();
    }
}
